﻿using System;
using System.Collections.Generic;
using HigLabo.Net.Extensions;

namespace HigLabo.Net.WindowsLive
{
    /// <summary>
    /// 
    /// </summary>
    public class OAuthToken : ResponseObject
    {
        private readonly List<String> _scopes = new List<string>();
        /// <summary>
        /// 
        /// </summary>
        public String AccessToken { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String AuthenticationToken { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Int32 ExpiresIn { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Scope { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String TokenType { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String RefreshToken { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<String> Scopes
        {
            get { return _scopes; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonText"></param>
        public OAuthToken(String jsonText)
        {
            var d = this.SetData(jsonText);
            this.AccessToken = d.ToString("access_token");
            this.AuthenticationToken = d.ToString("authentication_token");
            this.ExpiresIn = d.ToInt32("expires_in") ?? -1;
            this.Scope = d.ToString("scope");
            this.TokenType = d.ToString("token_type");
            this.RefreshToken = d.ToString("refresh_token");
            foreach (var s in this.Scope.Split(' '))
            {
                this.Scopes.Add(s);
            }
        }
    }
}
